package com.book.controller;

import com.book.pojo.Address;
import com.book.pojo.Book;
import com.book.pojo.Result;
import com.book.pojo.User;
import com.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    //增加
    @PostMapping("/")
    public Result add(@RequestBody User user){
        Integer count = userService.add(user);
        return new Result(true,"影响"+count+"条数据",count);
    }

    //增加多个用户(用户包含多个地址)
    @PostMapping("/addList")
    public Result addList(@RequestBody List<User> userList){
        Integer count = userService.addList(userList);
        return new Result(true,"影响"+count+"条数据",count);
    }

    //查询所有用户的地址
    @GetMapping("/")
    public Result findAll() {
        List<User> all = userService.findAll();
        return new Result(true,"查询成功",all);
    }
    //查询所有地址
    @GetMapping("/address/")
    public Result findAllAddress() {
        List<Address> all = userService.findAllAddress();
        return new Result(true,"查询成功",all);
    }
    //查询一个用户的地址
    @GetMapping("/{userId}")
    public List<Address> findByUserId(@PathVariable("userId") Integer userId) {
        return userService.findByUserId( userId );
    }
}
